import axios from 'axios'
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
axios.defaults.withCredentials = true // 配合 sever cookie 跨域
const service = axios.create({
  baseURL: 'http://localhost:8888',
  timeout: 30000,
})
service.interceptors.request.use(
  (config) => {
    // loadingBar.start();
    // const isToken = (config.headers || {}).isToken === false
    // const token = localStorage.getItem('t') || '';
    // if (!isToken && token) {
    //   token && (config.headers.Authorization = `Bearer ${token}`);
    // }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  (response) => {
    if (response.data.status === 200 || response.status === 201) {
      return response.data
    } else if (response.data.status === 403) {
      // Notify(response.data.msg);
      return response.data
    } else if (response.data.status === 401 || response.data.status === 404) {
      // Notify(response.data.msg);
      return Promise.reject(response)
    } else if (response.data === 401) {
      // Notify('身份过期！请重新登录, 5秒后退出');
      // setTimeout(() => {
      //   Store.commit('user/REMOVE_USER_INFO');
      //   router.push('/login');
      // }, 5000);
    } else if (response.data === 404) {
      // Notify('身份过期！请重新登录, 5秒后退出');
      // eslint-disable-next-line no-dupe-else-if
    } else if (response.data.status === 200) {
      return response
    }
    return response.data
  },
  (error) => {
    return Promise.reject(error)
  }
)

export default service
